package com.mindainfo.groundwave.server.interactive.redis.service;

import com.alibaba.fastjson.JSON;
import com.mindainfo.groundwave.common.bean.RedisConstant;
import com.mindainfo.groundwave.server.interactive.redis.bean.ZDInfo;
import com.mindainfo.groundwave.server.interactive.redis.bean.ZJInfo;
import org.apache.commons.lang3.StringUtils;
import org.omg.PortableInterceptor.INACTIVE;
import org.redisson.api.RBucket;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by Administrator on 2017/11/6.
 * 定时获取redis中的中继终端信息
 */
@Service
public class LoadInfoService {

    private Logger logger = LoggerFactory.getLogger("加载地波数据");

    @Resource
    private RedissonClient redissonClient;

    /**
     * 在上一次运行完成之后,隔一分钟执行下一次
     */
    @Scheduled(fixedDelay = 60000)
    public void loading(){

        logger.info("定时获取数据");
        RBucket<String> dbJzlist = redissonClient.getBucket(RedisConstant.DB_ZJLIST);
        String dbJzl = dbJzlist.get();
        if(StringUtils.isNotEmpty(dbJzl)){
            String[] zjs = dbJzl.split(",");
            for(String zj : zjs){
                String offsetKey = RedisConstant.DBZJ_RPTST_Receiver_OFFSET_ + zj;
                RBucket<String> offsetBucket = redissonClient.getBucket(offsetKey);
                String offset = offsetBucket.get();
                if(StringUtils.isNotEmpty(offset)){
                    String valueKey = RedisConstant.DBZJ_RPTST_Receiver_OFFSET_ + zj + "_" + offset;
                    RBucket<String> valueBucket = redissonClient.getBucket(valueKey);
                    String value = valueBucket.get();
                    ZJInfo zjInfo = JSON.parseObject(value, ZJInfo.class);
                    zjInfo.setLastTime(new Date());
                    String termSt = zjInfo.getTermSt();
                    String term1 = termSt.substring(0,8);
                    String term2 = termSt.substring(8,16);
                    String term3 = termSt.substring(16,24);
                    String term4 = termSt.substring(24,32);
                    String term5 = termSt.substring(32,40);
                    String term6 = termSt.substring(40,48);
                    String term7 = termSt.substring(48,56);
                    String term8 = termSt.substring(56);
                    zjInfo.getZdInfoList().add(parseIntZD(term1,1));
                    zjInfo.getZdInfoList().add(parseIntZD(term2,2));
                    zjInfo.getZdInfoList().add(parseIntZD(term3,3));
                    zjInfo.getZdInfoList().add(parseIntZD(term4,4));
                    zjInfo.getZdInfoList().add(parseIntZD(term5,5));
                    zjInfo.getZdInfoList().add(parseIntZD(term6,6));
                    zjInfo.getZdInfoList().add(parseIntZD(term7,7));
                    zjInfo.getZdInfoList().add(parseIntZD(term8,8));
                    RMap<String, ZJInfo> map = redissonClient.getMap(RedisConstant.web_zj_hash);
                    map.put(zjInfo.getDBZJid(),zjInfo);
                }
            }

        }
    }

    private ZDInfo parseIntZD(String termSt,Integer id){
        ZDInfo zdInfo = new ZDInfo();
        zdInfo.setId(id);
        String zdOnlineStr = termSt.substring(0, 2);
        String zdAlarmStr = termSt.substring(2,4);
        String zdElectricStr = termSt.substring(4,6);
        zdInfo.setOnLine(Integer.parseInt(zdOnlineStr));
        zdInfo.setAlarm(Integer.parseInt(zdAlarmStr));
        zdInfo.setElectric(Integer.parseInt(zdElectricStr));
        return zdInfo;
    }
}

